package com.clip.util.verifycode;

import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.clip.action.util.SessionAttributeKey;

/**
 * Servlet implementation class VerifyCodeServlet
 */
public class VerifyCodeServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	private final String UPPERLETTERO="O";
	private final String NUMBER0="0";
    /**
     * @see HttpServlet#HttpServlet()
     */
    public VerifyCodeServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
//		System.out.println("验证码...");
		//设置浏览器不缓存本页
        response.setHeader("Cache-Control", "no-cache");
        
        //生成验证码，写入用户session 
        String verifyCode=VerifyCode.generateTextCode(VerifyCode.TYPE_NUM_UPPER,4,null);
        if(verifyCode.indexOf(UPPERLETTERO)>=0||verifyCode.indexOf(NUMBER0)>=0){
        	verifyCode=VerifyCode.generateTextCode(VerifyCode.TYPE_NUM_UPPER,4,null);
        }
        request.getSession().setAttribute(SessionAttributeKey.VERIFYCODE,verifyCode);
//        System.out.println(verifyCode);
        //输出验证码给客户端
        response.setContentType("image/jpeg");
        BufferedImage bim=VerifyCode.generateImageCode(verifyCode, 90, 30, 3,true,Color.WHITE,Color.BLACK,null);
        ImageIO.write(bim, "JPEG",response.getOutputStream());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		this.doGet(request, response);
	}

}
